package com.gjintel.framework.role.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gjintel.framework.menu.model.RoleModel;
import com.gjintel.framework.role.dao.IRoleDao;
import com.gjintel.framework.role.model.ProjectOperation;
import com.gjintel.framework.role.model.Role;
import com.gjintel.framework.role.service.IRoleService;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.user.model.UserRole;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.base.service.impl.BaseServiceImpl;

/**
 * @description 角色业务实现类
 * @author fengya
 * @date 2015-7-23 下午04:37:09
 */
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl implements IRoleService {
	@Autowired
	private IRoleDao roleDao;

	@SuppressWarnings("unchecked")
	@Override
	public void findAll(Page page, Role role, Sort sort) {
		this.roleDao.findAll(page, role, sort);
	}

	@Override
	public void removeData(String deleteIds) throws Exception {
		Role role = null;
		for (String id : deleteIds.split(",")) {
			role = this.get(Role.class, id);
			if (role != null) {
				role.setFlag(1);
			}
		}
	}

	@Transactional
	@Override
	public List<RoleModel> bindMenu(User user,String roleId, String modelIdStr) {
		String[] modelIds = modelIdStr.split(",");
		List<RoleModel> rmList = new ArrayList<RoleModel>(modelIds.length);
		List<RoleModel> dbRmList = this.roleDao.getRoleModelByRoleId(roleId);
		this.remove(dbRmList);
		RoleModel rm = null;
		for (String modelId : modelIds) {
			if (!"".equals(modelId.trim())) {
				rm = new RoleModel();
				rm.setModelId(modelId);
				rm.setRoleId(roleId);
				rm.setCreaterId(user.getId());
				rm.setCreaterName(user.getName());
				rm.setCreateTime(new Date());
				rmList.add(rm);
			}
		}
		return rmList;
	}

	@Override
	public List<RoleModel> getMenuByRoleId(String roleId) {
		return this.roleDao.getRoleModelByRoleId(roleId);
	}

	@Override
	public void removeBindUser(String roleId, String userIdStr) {
		String[] userIds = userIdStr.split(",");
		for (String userId : userIds) {
			if (!"".equals(userId.trim())) {
				UserRole ur = this.roleDao.getUserRole(roleId, userId);
				if (ur != null) {
					this.remove(ur);
				}
			}
		}
	}

	@Override
	public List<Role> findAll() {
		return this.roleDao.findAll();
	}

	@Override
	public List<Role> findRoleListExcludeAdmin(String bidderId) {
		return this.roleDao.findRoleListExcludeAdmin(bidderId);
	}

	@Override
	public List<Role> findRolesByUserId(String userIdStr) {
		return this.roleDao.findRolesByUserId(userIdStr);
	}

	@Override
	public List<User> findUserByRoleId(String RoleId) {
		return this.roleDao.findUserByRoleId(RoleId);
	}

	@Override
	public boolean isPrivilege(String userId,String roleIds) {
		List<Role> roleList = this.findRolesByUserId(userId);
		for (Role role : roleList) {
			for (String roleId : roleIds.split(",")) {
				boolean flag = roleId.equals(role.getId());
				if (flag) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public List<ProjectOperation> findOperationByRoleIdAndStatus(String roleIds,
			String status,String workflowKey) {
		return this.roleDao.findOperationByRoleIdAndStatus(roleIds,status,workflowKey);
	}

	@Override
	public UserRole findUserRole(String roleStr , String userStr) {
		
		return this.roleDao.getUserRole(roleStr,userStr) ;
	}

	@Override
	public List<Role> getRoleListExcludeAdmin() {
		return this.roleDao.getRoleListExcludeAdmin();
	}

}
